package Encryption;


import javax.crypto.Cipher; 
import javax.crypto.spec.SecretKeySpec;


public class Aes 
{
	// key has to be 128 bits (16 bytes, where 1 letter = 1 byte)
	private static final String keyValue = "ThisIsAKeyThisIs";
	private static final byte[] keyInByte = keyValue.getBytes();
	private static final String aes = "AES";
	private static final String aesNoPad = "AES/ECB/NoPadding";
	private static final SecretKeySpec key = new SecretKeySpec(keyInByte, aes);
			
	public static byte[] EncryptedThis(String name, String r, int dh, String Kab) throws Exception
	{
		String dh_string = Integer.toString(dh);
		String input = name + "/" + r + "/" + dh_string + "/" + Kab;

		return Encrypt(input);
	}
	
	public static String beforeEncryption(String name, String r, int dh, String Kab){
		String dh_string = Integer.toString(dh);
		return (name + "/" + r + "/" + dh_string + "/" + Kab);
	}
		
	public static byte[] Encrypt(String plainText) throws Exception
	{
		final byte[] plainTextInByte = plainText.getBytes();
		
		Cipher c = Cipher.getInstance(aes);
		c.init(Cipher.ENCRYPT_MODE, key);

		byte[] encryptedByte = new byte[c.getOutputSize(plainTextInByte.length)];
		int length = c.update(plainTextInByte, 0, plainTextInByte.length, encryptedByte, 0);
		length += c.doFinal(encryptedByte, length);
		
		String encryptedText = new String(encryptedByte);
		
		//System.out.println("Plain: " + plainText + " Encrytped: " + encryptedText);

		return encryptedByte;
	}
	
	public static String Decrypt(byte[] encryptedByte) throws Exception
	{    
				
		Cipher c = Cipher.getInstance(aes);
		c.init(Cipher.DECRYPT_MODE, key);
		
		int encryptedTextlength = encryptedByte.length;
		encryptedTextlength += c.doFinal(encryptedByte, encryptedTextlength);
		
		byte[] plainTextInByte = new byte[c.getOutputSize(encryptedTextlength)];
		
		int plainTextLength = c.update(encryptedByte, 0, encryptedTextlength, plainTextInByte, 0);
		
		plainTextLength += c.doFinal(plainTextInByte, plainTextLength);
		
		String plainText = new String (plainTextInByte);
		String encryptedText = new String(encryptedByte);
		
		//System.out.println("Encrypted: " + encryptedText + " Plain: " + plainText);

		return plainText;
		
	}
	
	public static String DecryptNoPadding(String encryptedS) throws Exception
	{
		//AES/ECB/NoPadding
		Cipher c = Cipher.getInstance(aesNoPad);
		c.init(Cipher.DECRYPT_MODE, key);
		
		byte[] encryptedB = encryptedS.getBytes();
		
		int encryptedB_length = encryptedB.length;
		encryptedB_length += c.doFinal(encryptedB, encryptedB_length);
		
		byte[] plainTextB = new byte[c.getOutputSize(encryptedB_length)];
		
		int plainTextB_length = c.update(encryptedB, 0, encryptedB_length, plainTextB, 0);
		
		plainTextB_length += c.doFinal(plainTextB,  plainTextB_length);
		
		String plainText = new String (plainTextB);
		
		//System.out.println("Encrypted String: " + encryptedS + " Plain: " + plainText);
		
		return plainText;
	}

	
}
